<script setup>
// import { updateUserInfoServer } from '@/api/user.js'
import { useUserStore } from '@/stores/index.js'
import { ref } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import { updateUserInfoServer } from '@/api/user.js'
const formRef = ref(null)
const {
  userData: { id, username, nickname, email },
  setUserData
} = useUserStore()

const form = ref({
  id,
  username, // 示例的禁用登录名称
  nickname,
  email
})

const rules = {
  nickname: [
    { required: true, message: '昵称不能为空', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '昵称必须是1-10位非空格字符',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: ['blur', 'change'] }
  ]
}

const submit = async () => {
  formRef.value.validate()
  await updateUserInfoServer(form.value)
  await setUserData()
  ElMessage.success('修改成功')
}
</script>

<template>
  <page-container title="基本资料">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
      <el-form-item label="登录名称">
        <el-input v-model="form.username" disabled></el-input>
      </el-form-item>

      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>

      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit">提交修改</el-button>
      </el-form-item>
    </el-form>
  </page-container>
</template>

<style scoped>
.el-input {
  width: 400px;
}
</style>
